<template>
    <div class="root">
        <h1 style="background-color: antiquewhite;">Search Image Users</h1>
        <div class="ddd">
            <input type="text" placeholder="enter user name" v-model="searchContent">
            <button @click="searchClick">search</button>
        </div>
    </div>

</template>

<script>
import axios from 'axios'
export default {
    data() {
        return {
            searchContent: ''
        }
    },
    watch: {
        userName(nueVal) {
            console.log(nueVal)
        }
    },
    methods: {
        searchClick() {
            if(this.searchContent.length === 0){
                alert("请输入要搜索的图片");
                return
            }
            const listObj = {
                list: [],
                shwoWelcom: false,
                shwoComing: true,
                errorMsg: "",
            }
            this.$bus.$emit('getListObj', listObj)


            axios.defaults.baseURL = 'https://pixabay.com';
            axios.get(`/api/?key=41096223-f200f1326a8253bc9264e6e3b&q=${this.searchContent}`)
                .then(res => {
                    console.log(res.data.hits)
                    const listObj = {
                        list: res.data.hits,
                        shwoWelcom: false,
                        shwoComing: false,
                        errorMsg: "",
                    }
                    this.$bus.$emit('getListObj', listObj)
                }).catch(e => {
                    const listObj = {
                        list: [],
                        shwoWelcom: false,
                        shwoComing: false,
                        errorMsg: e.message,
                    }
                    this.$bus.$emit('getListObj', listObj)
                    console.log('---------',e.message);
                })
        }
    }
}
</script>

<style lang="less" scoped>
.root {
    height: 100px;
    background-color: darkgray;
    display: flex;
    padding: 10px;
    flex-direction: column;
    justify-content: center;
    gap: 5px;

    h1 {
        margin: 0px;
    }
}
</style>